// @Date         2021-11-19
// @Author       Aaron Zhang
// @Description
// @FileName      env.go

package command

import (
	"fmt"
	"zin/framework/cobra"
	"zin/framework/util"
)

func initEnvCommand() *cobra.Command {
	envCommand.AddCommand(envListCommand)
	return envCommand
}

var envCommand = &cobra.Command{
	Use:   "env",
	Short: "获取环境用，无参返回app_env,有参数反馈对应值 env test",
	RunE: func(cmd *cobra.Command, args []string) error {
		env := util.GetEnvProvider()

		if len(args) == 0 {
			//获取当前环境
			fmt.Println("当前环境变量为：" + env.Env())
		} else {
			key := args[0]
			fmt.Printf("环境变量%s的值为：%s\n", key, env.Get(key))
		}

		return nil
	},
}

var envListCommand = &cobra.Command{
	Use:   "list",
	Short: "列出让你额环境变量",
	RunE: func(cmd *cobra.Command, args []string) error {
		app := util.GetEnvProvider()
		for k, v := range app.GetAll() {
			fmt.Printf("%s = %s \n ", k, v)
		}
		return nil
	},
}
